from selenium.webdriver.common.by import By
from base.base_page import BasePage,BaseHandle

#对象库层
class addAdminPage(BasePage):
    def __init__(self):
        super().__init__()    # 初始化 driver
        #表单外
        self.setting_button=By.XPATH,"/html/body/div[1]/div[3]/ul/li[2]/a"
        self.list_button=By.LINK_TEXT,"管理员列表"
        # 表单内
        self.addAdmin_button=By.CLASS_NAME,"add"
        self.username=By.NAME,"user_name"
        self.email=By.NAME,"email"
        self.password=By.NAME,"password"
        self.role=By.NAME,"role_id"
        self.submitBtn=By.ID,"submitBtn"

    def SwitchFrame(self, ID):    #切换进iframe表单
        self.driver.switch_to.frame(ID)

    def find_setting(self):
        return self.get_element(self.setting_button)

    def find_list(self):
        return self.get_element(self.list_button)

    def find_addAdmin(self):
        return self.get_element(self.addAdmin_button)

    def find_username(self):
        return self.get_element(self.username)

    def find_email(self):
        return self.get_element(self.email)

    def find_password(self):
        return self.get_element(self.password)

    def find_role(self):
        return self.get_element(self.role)

    def find_submitBtn(self):
        return self.get_element(self.submitBtn)


#操作层
class addAdminHandle(BaseHandle):
    def __init__(self):
        self.addAdmin_page=addAdminPage()
        self.id = "workspace"

    #封装操作
    def switch_frame(self):
        id=self.id
        self.addAdmin_page.SwitchFrame(id)

    def click_setting(self):
        self.addAdmin_page.find_setting().click()

    def click_list(self):
        self.addAdmin_page.find_list().click()

    def click_addAdmin(self):
        self.addAdmin_page.find_addAdmin().click()

    def input_username(self,username):
        self.input_text(self.addAdmin_page.find_username(),username)

    def input_email(self,email):
        self.input_text(self.addAdmin_page.find_email(),email)

    def input_password(self,password):
        self.input_text(self.addAdmin_page.find_password(),password)

    def input_role(self,role):
        self.input_text(self.addAdmin_page.find_role(),role)

    def click_submitBtn(self):
        self.addAdmin_page.find_submitBtn().click()



#业务层
class addAdminProxy:
    def __init__(self):
        self.addAdmin_Handle=addAdminHandle()

    def addAdmin(self,username,email,password):
        self.addAdmin_Handle.click_setting()
        self.addAdmin_Handle.click_list()
        self.addAdmin_Handle.switch_frame()
        self.addAdmin_Handle.click_addAdmin()
        self.addAdmin_Handle.input_username(username)
        self.addAdmin_Handle.input_email(email)
        self.addAdmin_Handle.input_password(password)
        self.addAdmin_Handle.click_submitBtn()


